/**
 * 
 */
package com.yst.metho.web.application.projectversion.actions.wbs;

import nextapp.echo2.app.Extent;
import nextapp.echo2.app.SelectField;
import nextapp.echo2.app.event.ActionEvent;

import com.yst.metho.web.application.SuiviProdApp;
import com.yst.metho.web.application.components.GenericEditWindow;
import com.yst.metho.web.application.components.listingview.ToolbarAction;
import com.yst.metho.web.application.reports.GenerateReportAction;
import com.yst.metho.web.application.reports.ReportGenerationPane;
import com.yst.metho.web.application.reports.ReportListModel;
import com.yst.metho.web.application.utils.ExceptionMgt;
import com.yst.suivi.data.project.Project;
import com.yst.suivi.data.project.versions.ProjectVersion;
import com.yst.suivi.data.reports.ReportBean;
import com.yst.suivi.data.rights.Right;
import com.yst.suivi.status.StatusConstants;

/**
 * @author perso
 * 
 */
public class ReportActivityAction extends ToolbarAction
{
	public static final Extent ACTIVITY_HEIGHT = new Extent(250, Extent.PX);

	private SelectField mVersionProject;

	private Project mPrj;

	/**
	 * 
	 */
	private static final String CREATE_WBS_COMMAND = "ReportActivity";

	public ReportActivityAction(SelectField versionProject, Project prj)
	{
		super(Right.REPORT_ACTIVITY, "Rapport");
		mPrj = prj;
		mVersionProject = versionProject;
		setActionCommand(CREATE_WBS_COMMAND);
		addActionListener(this);
		if (StatusConstants.CLOSE == mPrj.getStatus())
		{
			setEnabled(false);
		}
	}

	public void actionPerformed(ActionEvent e)
	{
		if (CREATE_WBS_COMMAND.equals(e.getActionCommand()))
		{
			// verification qu'une version du projet est selectionne.
			if (-1 != mVersionProject.getSelectedIndex())
			{

				// String reportUrl = "report?rapport={0}&version={1}";
				// String[] params = new String[2];
				// params[0] = "2";
				// params[1] = ((ProjectVersion)
				// mVersionProject.getSelectedItem()).getId();
				// ApplicationInstance.getActive().enqueueCommand(new
				// BrowserRedirectCommand(MessageFormat.format(reportUrl,
				// params)));
				try
				{
					ReportListModel model = new ReportListModel(ReportBean.PROJECT_REPORT);
					model.refresh();
					ReportGenerationPane view = new ReportGenerationPane(model);
					view.setPrimaryKey(((ProjectVersion) mVersionProject.getSelectedItem()).getId());
					GenerateReportAction btn = new GenerateReportAction(model, view);
					view.getSelectionModel().addChangeListener(btn);
					GenericEditWindow window = new GenericEditWindow(view, "Generation d'un rapport", btn);
					window.setHeight(new Extent(230, Extent.PX));
					SuiviProdApp.getApp().getDefaultWindow().getContent().add(window);
				}
				catch (Throwable t)
				{
					ExceptionMgt.manage(t);
				}
			}
			else
			{
				ExceptionMgt.displayMessageBox("Vous devez selectionner une version du projet");
			}
		}
	}
}
